# !/user/bin/env python3
# -*- coding:utf-8 -*-
import logging.handlers
import time


# 初始化日志的配置
from use import get_path


def init_log_config():
    # 创建日志器对象
    logger = logging.getLogger()
    logger.handlers.clear()
    logger.setLevel(logging.INFO)

    # 创建处理器对象
    sh = logging.StreamHandler()
    nowtimes = time.strftime('%Y%m%d_%H', time.localtime(time.time()))
    log_path = get_path()+"/logs/{}_test.log".format(nowtimes)
    fh = logging.handlers.TimedRotatingFileHandler(log_path, when="midnight", interval=1, backupCount=15,
                                                   encoding="UTF-8")

    # 创建格式化器对象
    fmt = "%(asctime)s %(levelname)s [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s"
    formatter = logging.Formatter(fmt=fmt)

    # 把格式化器添加到处理器中
    sh.setFormatter(formatter)
    fh.setFormatter(formatter)

    # 把处理器添加到日志器中
    logger.addHandler(sh)
    logger.addHandler(fh)
    logger.propagate = 0


init_log_config()